package top.yuhaitao.websocket;

import java.io.IOException;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/hello")
public class WsPool {

	@OnOpen
	public void onopen(Session session) {
		System.out.println("连接成功");
		try {
			session.getBasicRemote().sendText("hello client...");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@OnClose
	public void onclose(Session session) {
		System.out.println("close....");

	}

	@OnMessage
	public void onsend(Session session, String msg) throws InterruptedException {
		try {
			while (true) {
				Thread.sleep(3000);
				System.out.println("server send ....");
				session.getBasicRemote().sendText("client-" + session.getId() + "-say:" + msg);
				session.close();

			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
